<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          type="primary"
          @click="handleSearchList()"
          size="small"
        >
          查询搜索
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="用户名：">
            <el-input
              v-model="listQuery.userName"
              class="input-width"
              placeholder="用户名"
              clearable
            ></el-input>
          </el-form-item>

          <el-form-item label="手机号：">
            <el-input
              v-model="listQuery.phone"
              class="input-width"
              placeholder="手机号"
              clearable
            ></el-input>
          </el-form-item>

          <el-form-item label="组织编码：" prop="organizationId">
            <el-cascader
              placeholder="组织编码"
              :options="orgList"
              :props="{
                children: 'childs',
                label: 'organizationName',
                value: 'organizationCode',
                emitPath: false,
              }"
              v-model="listQuery.organizationCode"
              :show-all-levels="false"
              change-on-select
              style="width: 100%"
              clearable
            />
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <div class="table-container">
      <el-table
        ref="adminTable"
        :data="list"
        style="width: 100%"
        v-loading="listLoading"
        border
      >
        <el-table-column label="手机号" align="center" prop="phone">
        </el-table-column>
        <el-table-column label="用户名" align="center" prop="username">
        </el-table-column>
        <el-table-column label="昵称" align="center" props="nickName">
        </el-table-column>
        <el-table-column label="性别" align="center">
          <template slot-scope="scope">{{
            scope.row.gender | formatGender
          }}</template>
        </el-table-column>
        <el-table-column label="生日" width="160" align="center">
          <template slot-scope="scope">{{
            scope.row.birthday | formatDateTime
          }}</template>
        </el-table-column>
        <el-table-column label="注册时间" width="160" align="center">
          <template slot-scope="scope">{{
            scope.row.createdAt | formatDateTime
          }}</template>
        </el-table-column>

        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              slot="reference"
              size="mini"
              type="text"
              @click="handleResetUserPwd(scope.row.id, scope.row.phone)"
              >重置密码
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="PAGE_SIZES"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { formatDate } from "@/utils/date";
import { httpOrgListOrg } from "@/api/org";
import { fetchUserList, resetUserPwd } from "@/api/mms";
import { formatGender } from "@/filters/format";
import { DEFAULT_PAGE_SIZE, PAGE_SIZES } from "@/utils/constant/system";

const defaultListQuery = {
  pageNum: 1,
  pageSize: DEFAULT_PAGE_SIZE,
  organizationCode: undefined,
  phone: "",
  userName: "",
};

export default {
  name: "adminList",
  data() {
    return {
      PAGE_SIZES,
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      listLoading: false,
      orgList: [],
    };
  },
  created() {
    this.getList();
    this.getOrgList();
  },
  filters: {
    formatDateTime(time) {
      if (time == null || time === "") {
        return "";
      }
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
    },
    formatGender,
  },
  methods: {
    getOrgList() {
      httpOrgListOrg().then((res) => {
        if (res.code === 200) {
          this.orgList = res.data;
        }
      });
    },
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleResetUserPwd(id, phone) {
      this.$confirm(
        `确定重置手机号：<span style="color:#E6A23C">${phone}</span> 的登录密码吗?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          dangerouslyUseHTMLString: true,
        },
      ).then(() => {
        resetUserPwd(id)
          .then(() => {
            this.$message({
              type: "success",
              message: "重置成功!",
            });
          })
          .catch(() => {
            this.$message({
              type: "error",
              message: "重置失败，请重试",
            });
          });
      });
    },

    getList() {
      this.listLoading = true;
      fetchUserList(this.listQuery).then((response) => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },
  },
};
</script>
